{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0a395b45",
   "metadata": {},
   "outputs": [],
   "source": [
    "deployment = \"gpt4\"\n",
    "embedding_deployment = \"embedding\"\n",
    "model = \"gpt4\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5e34a15c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: langchain_experimental in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (0.0.10)\n",
      "Requirement already satisfied: langchain>=0.0.239 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from langchain_experimental) (0.0.285)\n",
      "Requirement already satisfied: PyYAML>=5.3 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from langchain>=0.0.239->langchain_experimental) (6.0)\n",
      "Requirement already satisfied: SQLAlchemy<3,>=1.4 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from langchain>=0.0.239->langchain_experimental) (1.4.39)\n",
      "Requirement already satisfied: aiohttp<4.0.0,>=3.8.3 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from langchain>=0.0.239->langchain_experimental) (3.8.3)\n",
      "Requirement already satisfied: dataclasses-json<0.6.0,>=0.5.7 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from langchain>=0.0.239->langchain_experimental) (0.5.14)\n",
      "Requirement already satisfied: langsmith<0.1.0,>=0.0.21 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from langchain>=0.0.239->langchain_experimental) (0.0.26)\n",
      "Requirement already satisfied: numexpr<3.0.0,>=2.8.4 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from langchain>=0.0.239->langchain_experimental) (2.8.4)\n",
      "Requirement already satisfied: numpy<2,>=1 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from langchain>=0.0.239->langchain_experimental) (1.24.3)\n",
      "Requirement already satisfied: pydantic<3,>=1 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from langchain>=0.0.239->langchain_experimental) (1.10.12)\n",
      "Requirement already satisfied: requests<3,>=2 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from langchain>=0.0.239->langchain_experimental) (2.31.0)\n",
      "Requirement already satisfied: tenacity<9.0.0,>=8.1.0 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from langchain>=0.0.239->langchain_experimental) (8.2.2)\n",
      "Requirement already satisfied: attrs>=17.3.0 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain>=0.0.239->langchain_experimental) (22.1.0)\n",
      "Requirement already satisfied: charset-normalizer<3.0,>=2.0 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain>=0.0.239->langchain_experimental) (2.0.4)\n",
      "Requirement already satisfied: multidict<7.0,>=4.5 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain>=0.0.239->langchain_experimental) (6.0.2)\n",
      "Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain>=0.0.239->langchain_experimental) (4.0.2)\n",
      "Requirement already satisfied: yarl<2.0,>=1.0 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain>=0.0.239->langchain_experimental) (1.8.1)\n",
      "Requirement already satisfied: frozenlist>=1.1.1 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain>=0.0.239->langchain_experimental) (1.3.3)\n",
      "Requirement already satisfied: aiosignal>=1.1.2 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain>=0.0.239->langchain_experimental) (1.2.0)\n",
      "Requirement already satisfied: marshmallow<4.0.0,>=3.18.0 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from dataclasses-json<0.6.0,>=0.5.7->langchain>=0.0.239->langchain_experimental) (3.20.1)\n",
      "Requirement already satisfied: typing-inspect<1,>=0.4.0 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from dataclasses-json<0.6.0,>=0.5.7->langchain>=0.0.239->langchain_experimental) (0.9.0)\n",
      "Requirement already satisfied: typing-extensions>=4.2.0 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from pydantic<3,>=1->langchain>=0.0.239->langchain_experimental) (4.7.1)\n",
      "Requirement already satisfied: idna<4,>=2.5 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from requests<3,>=2->langchain>=0.0.239->langchain_experimental) (3.4)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from requests<3,>=2->langchain>=0.0.239->langchain_experimental) (1.26.16)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from requests<3,>=2->langchain>=0.0.239->langchain_experimental) (2023.7.22)\n",
      "Requirement already satisfied: packaging>=17.0 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from marshmallow<4.0.0,>=3.18.0->dataclasses-json<0.6.0,>=0.5.7->langchain>=0.0.239->langchain_experimental) (23.0)\n",
      "Requirement already satisfied: mypy-extensions>=0.3.0 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from typing-inspect<1,>=0.4.0->dataclasses-json<0.6.0,>=0.5.7->langchain>=0.0.239->langchain_experimental) (0.4.3)\n"
     ]
    }
   ],
   "source": [
    "!pip install langchain_experimental"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "c7977368",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_current_cluster_state(cluster_name):\n",
    "    return  \"\"\"ERROR: Failed to pull image \"chaocai/docker/dsp:latest\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "ad5b03fb",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.agents import initialize_agent, Tool\n",
    "from langchain_experimental.plan_and_execute import PlanAndExecute, load_agent_executor, load_chat_planner\n",
    "from langchain.chat_models import AzureChatOpenAI\n",
    "from langchain.chains import LLMRequestsChain\n",
    "from langchain.chains import LLMChain\n",
    "from langchain.prompts import PromptTemplate\n",
    "import time\n",
    "\n",
    "llm = AzureChatOpenAI(deployment_name = deployment, model_name=model, temperature=0, max_tokens=200)\n",
    "tools = [\n",
    "    Tool(\n",
    "        name=\"Get current cluster state\", func=get_current_cluster_state,\n",
    "        description=\"\"\"invoke it for when you need to get the current state in a given service cluster, \n",
    "        And the input of the function is only one parameter, the service name, for example, to get the state of ADX service, \n",
    "        the input for invoking the function is \"ADX\"         \n",
    "        \"\"\")\n",
    "]\n",
    "\n",
    "planner = load_chat_planner(llm)\n",
    "executor = load_agent_executor(llm, tools, verbose=True)\n",
    "agent = PlanAndExecute(planner=planner, executor=executor, verbose=True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "c317606a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new PlanAndExecute chain...\u001b[0m\n",
      "steps=[Step(value=\"Identify the service named 'DSP'.\"), Step(value=\"Check the status of the 'DSP' service.\"), Step(value='If the service is running correctly, inform the user that there is no issue with the service.'), Step(value='If the service is not running correctly, diagnose the problem.'), Step(value='Based on the diagnosis, suggest possible solutions to fix the issue.'), Step(value=\"Given the above steps taken, respond to the user's original request by providing the status of the 'DSP' service and any necessary solutions to fix it, if applicable.\\n\")]\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3mThought: The user has not provided the name of the DSP service yet. I need to ask for it again to proceed with the task.\n",
      "Action:\n",
      "```\n",
      "{\n",
      "  \"action\": \"Final Answer\",\n",
      "  \"action_input\": \"Could you please specify the name of the DSP service that you want to check?\"\n",
      "}\n",
      "```\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "*****\n",
      "\n",
      "Step: Identify the service named 'DSP'.\n",
      "\n",
      "Response: Could you please specify the name of the DSP service that you want to check?\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3mThought: The user wants to check the status of the 'DSP' service. I will use the 'Get current cluster state' tool to get the current state of the 'DSP' service.\n",
      "\n",
      "Action:\n",
      "```\n",
      "{\n",
      "  \"action\": \"Get current cluster state\",\n",
      "  \"action_input\": \"DSP\"\n",
      "}\n",
      "```\u001b[0m\n",
      "Observation: \u001b[36;1m\u001b[1;3mERROR: Failed to pull image \"chaocai/docker/dsp:latest\u001b[0m\n",
      "Thought:\u001b[32;1m\u001b[1;3mThe 'Get current cluster state' tool returned an error indicating that it failed to pull the latest image for the 'DSP' service. This could be due to a variety of reasons such as network issues, incorrect image name, or the image not being available. The user should verify the image name and availability. If the problem persists, they may need to check their network connection or contact their system administrator. \n",
      "\n",
      "Action:\n",
      "```\n",
      "{\n",
      "  \"action\": \"Final Answer\",\n",
      "  \"action_input\": \"The tool returned an error indicating that it failed to pull the latest image for the 'DSP' service. This could be due to a variety of reasons such as network issues, incorrect image name, or the image not being available. Please verify the image name and availability. If the problem persists, you may need to check your network connection or contact your system administrator.\"\n",
      "}\n",
      "```\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "*****\n",
      "\n",
      "Step: Check the status of the 'DSP' service.\n",
      "\n",
      "Response: The tool returned an error indicating that it failed to pull the latest image for the 'DSP' service. This could be due to a variety of reasons such as network issues, incorrect image name, or the image not being available. Please verify the image name and availability. If the problem persists, you may need to check your network connection or contact your system administrator.\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3mThought: The user has not specified the name of the DSP service that needs to be checked. However, the previous steps indicate that there was an error pulling the latest image for the 'DSP' service. I need to check the current state of the DSP service to provide an accurate response. \n",
      "\n",
      "Action:\n",
      "```\n",
      "{\n",
      "  \"action\": \"Get current cluster state\",\n",
      "  \"action_input\": \"DSP\"\n",
      "}\n",
      "```\u001b[0m\n",
      "Observation: \u001b[36;1m\u001b[1;3mERROR: Failed to pull image \"chaocai/docker/dsp:latest\u001b[0m\n",
      "Thought:\u001b[32;1m\u001b[1;3mThe observation indicates that there was an error pulling the latest image for the DSP service. This could be due to a variety of reasons such as network issues, incorrect image name, or the image not being available. I will inform the user about this issue.\n",
      "\n",
      "Action:\n",
      "```\n",
      "{\n",
      "  \"action\": \"Final Answer\",\n",
      "  \"action_input\": \"There seems to be an issue with the DSP service. The system failed to pull the latest image for the DSP service. This could be due to network issues, incorrect image name, or the image not being available. Please verify the image name and availability. If the problem persists, you may need to check your network connection or contact your system administrator.\"\n",
      "}\n",
      "```\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "*****\n",
      "\n",
      "Step: If the service is running correctly, inform the user that there is no issue with the service.\n",
      "\n",
      "Response: There seems to be an issue with the DSP service. The system failed to pull the latest image for the DSP service. This could be due to network issues, incorrect image name, or the image not being available. Please verify the image name and availability. If the problem persists, you may need to check your network connection or contact your system administrator.\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3mThought: The previous steps indicate that there is an issue with the DSP service as the system failed to pull the latest image for it. This could be due to network issues, incorrect image name, or the image not being available. To diagnose the problem, we need to check the current state of the DSP service. \n",
      "Action:\n",
      "```\n",
      "{\n",
      "  \"action\": \"Get current cluster state\",\n",
      "  \"action_input\": \"DSP\"\n",
      "}\n",
      "```\u001b[0m\n",
      "Observation: \u001b[36;1m\u001b[1;3mERROR: Failed to pull image \"chaocai/docker/dsp:latest\u001b[0m\n",
      "Thought:\u001b[32;1m\u001b[1;3mThe error message indicates that the system failed to pull the latest image for the DSP service from the specified location. This could be due to a variety of reasons such as network issues, incorrect image name, or the image not being available. The user should verify the image name and its availability. If the problem persists, they may need to check their network connection or contact their system administrator. \n",
      "Action:\n",
      "```\n",
      "{\n",
      "  \"action\": \"Final Answer\",\n",
      "  \"action_input\": \"The system failed to pull the latest image for the DSP service. This could be due to network issues, incorrect image name, or the image not being available. Please verify the image name and availability. If the problem persists, you may need to check your network connection or contact your system administrator.\"\n",
      "}\n",
      "```\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "*****\n",
      "\n",
      "Step: If the service is not running correctly, diagnose the problem.\n",
      "\n",
      "Response: The system failed to pull the latest image for the DSP service. This could be due to network issues, incorrect image name, or the image not being available. Please verify the image name and availability. If the problem persists, you may need to check your network connection or contact your system administrator.\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3mThought: The user has not provided the name of the DSP service that needs to be checked. However, based on the previous responses, it seems there is an issue with pulling the latest image for the DSP service. This could be due to network issues, incorrect image name, or the image not being available. The suggested solutions would be to verify the image name and availability, check the network connection, or contact the system administrator. \n",
      "\n",
      "Action:\n",
      "```\n",
      "{\n",
      "  \"action\": \"Final Answer\",\n",
      "  \"action_input\": \"There seems to be an issue with pulling the latest image for the DSP service. This could be due to network issues, incorrect image name, or the image not being available. I suggest you to verify the image name and availability. If the problem persists, you may need to check your network connection or contact your system administrator.\"\n",
      "}\n",
      "```\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "*****\n",
      "\n",
      "Step: Based on the diagnosis, suggest possible solutions to fix the issue.\n",
      "\n",
      "Response: There seems to be an issue with pulling the latest image for the DSP service. This could be due to network issues, incorrect image name, or the image not being available. I suggest you to verify the image name and availability. If the problem persists, you may need to check your network connection or contact your system administrator.\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3mThought: The user has not provided a specific DSP service to check. However, based on the previous steps, it seems there is an issue with pulling the latest image for a DSP service. The suggested solution was to verify the image name and availability, check the network connection, or contact the system administrator. I will reiterate this information in my response.\n",
      "\n",
      "Action:\n",
      "```\n",
      "{\n",
      "  \"action\": \"Final Answer\",\n",
      "  \"action_input\": \"There seems to be an issue with pulling the latest image for the DSP service. This could be due to network issues, incorrect image name, or the image not being available. I suggest you to verify the image name and availability. If the problem persists, you may need to check your network connection or contact your system administrator.\"\n",
      "}\n",
      "```\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "*****\n",
      "\n",
      "Step: Given the above steps taken, respond to the user's original request by providing the status of the 'DSP' service and any necessary solutions to fix it, if applicable.\n",
      "\n",
      "\n",
      "Response: There seems to be an issue with pulling the latest image for the DSP service. This could be due to network issues, incorrect image name, or the image not being available. I suggest you to verify the image name and availability. If the problem persists, you may need to check your network connection or contact your system administrator.\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'There seems to be an issue with pulling the latest image for the DSP service. This could be due to network issues, incorrect image name, or the image not being available. I suggest you to verify the image name and availability. If the problem persists, you may need to check your network connection or contact your system administrator.'"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agent.run(\"Check the service named 'DSP', if there's something wrong with the service, give me some suggestions to fix it.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4924645b",
   "metadata": {},
   "source": [
    "# 文本向量化（embedding)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "04d04b72",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.embeddings.openai import OpenAIEmbeddings\n",
    "# embedding = OpenAIEmbeddings() #如果直接使用OpenAI的GPT服务\n",
    "embedding = OpenAIEmbeddings(deployment=embedding_deployment) #deployment是你在Azure中的 embedding 模型的部署名字\n",
    "sentence1 = \"我是一名软件工程师\"\n",
    "sentence2 = \"小张从事法律工作\"\n",
    "sentence3 = \"我是一名程序员\"\n",
    "\n",
    "embedding1 = embedding.embed_query(sentence1)\n",
    "embedding2 = embedding.embed_query(sentence2)\n",
    "embedding3 = embedding.embed_query(sentence3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a9e09b2c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: numpy in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (1.24.3)\r\n"
     ]
    }
   ],
   "source": [
    "!pip install numpy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "dbf108f7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.7987662070931474\n",
      "0.7992461569419606\n",
      "0.9592105615669977\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "print(np.dot(embedding1,embedding2))\n",
    "print(np.dot(embedding2,embedding3))\n",
    "print(np.dot(embedding1,embedding3))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f668a883",
   "metadata": {},
   "source": [
    "# 处理大文本"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a5ba937",
   "metadata": {},
   "source": [
    "## 切割"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f84092e5",
   "metadata": {},
   "outputs": [],
   "source": [
    "def split_file_into_chunks(file_path, chunk_size):\n",
    "    chunks = []\n",
    "    with open(file_path, 'r') as file:\n",
    "        while True:\n",
    "            chunk = file.read(chunk_size)\n",
    "            if not chunk:\n",
    "                break\n",
    "            chunks.append(chunk)\n",
    "    return chunks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "688f8247",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['SpotMax\\n打破成本约束，释放云端创造力\\n\\n构建大规模云端系统的挑战\\n云计算平台“按需获取”的特性使得用户可以随时获取所需计算资源，实现计算资源与业务规模的动态匹配。云计算时代计算资源已不再是构建',\n",
       " '大规模系统的瓶颈，多数情况下成本成为了企业在构建大规模应用时的最大挑战。\\n\\n什么是 SpotMax?\\nSpotMax是一个利用弹性计算资源进行云端系统成本优化的体系（包括相关产品及解决方案）。Spot',\n",
       " 'Max旨在帮助用户使用云平台提供的弹性资源（如：AWS Spot Instance）在保证系统可用性，甚至获得更高可用性的前提下，实现成本的最大化节省。SpotMax来自于Mobvista降低成本的成',\n",
       " '功实践。\\nSpotMax由以下三个部分组成：\\nMaxArch：构建高可用性，高弹性（Resilient）架构，可以允许系统在不影响业务的前提下更充分的使用具有成本优势的高弹性计算资源（Spot Ins',\n",
       " 'tance）。\\nMaxChaos：利用混沌工程可以有效的评估系统的容错能力，其评估结果将成为MaxGroup自动规划合理弹性集群的依据。实现MaxArch的持续改进及评估。\\nMaxGroup：根据服务',\n",
       " '工作模式和运行特征，优化主机群集。智能弹性集群管理可以有效减少弹性计算资源波动（如：竞价实例的中断）带来的集群服务能力变化。并在面向成本优化构建的混合机型集群最大发挥集群不同机型的服务能力\\n以上各部分',\n",
       " '将以咨询服务和软件产品的形式提供。\\nMaxGroup 核心功能\\nMaxGroup可以根据服务的工作模式和运行时特征，对主机集群的组成提出优化建议（如预留实例、按需实例与Spot型实例的比率，Spot实',\n",
       " '例池等），并估算出此优化节省的成本。\\n用户确认优化集群方案后，自动实现新集群的构建或原有集群的构成的直接优化(安全替换现有集群的运行实例)。\\n通过对集群中Spot instance中断的预先补偿, 降',\n",
       " '低中断带来的服务器容量变化带来的服务能力影响，实现服务及数据的提前迁移。\\nMaxGroup将根据现货价格和终止率的变化持续优化集群结构。 \\n支持智能的动态负载均衡，由于集群为多机型混合，不同机型的处理',\n",
       " '能力有差异，智能的动态负载均衡将实现按处理能力的负载分配，实现集群资源利用的最大化。\\nMaxGroup可以根据工作负载变化模式（缓慢变化或急剧变化）和业务需求为系统自动伸缩选择合适的实例类型。 \\nMa',\n",
       " 'xGroup可以与Kubernetes和Consul无缝集成。实现容器环境下的中断预补偿机制。']"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "split_file_into_chunks(\"spotmax_intro.txt\",100)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "73529765",
   "metadata": {},
   "source": [
    "## 向量化 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "60d79a6f",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.embeddings.openai import OpenAIEmbeddings\n",
    "\n",
    "def embed(chunks): \n",
    "    # embedding = OpenAIEmbeddings()#如果直接使用OpenAI的GPT服务\n",
    "    embedding = OpenAIEmbeddings(deployment=embedding_deployment)\n",
    "    return [embedding.embed_query(chunk) for chunk in chunks]\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "a629739b",
   "metadata": {},
   "outputs": [],
   "source": [
    "chunks = split_file_into_chunks(\"spotmax_intro.txt\",100)\n",
    "embeddeds = embed(chunks)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d10edb6",
   "metadata": {},
   "source": [
    "## 向量检索"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "10087fbf",
   "metadata": {},
   "outputs": [],
   "source": [
    "def find_k_largest_indices(input_list, k):\n",
    "    sorted_indices = sorted(range(len(input_list)), key=lambda i: input_list[i], reverse=True)\n",
    "    print(input_list)\n",
    "    print(sorted_indices)\n",
    "    return sorted_indices[:k]\n",
    "\n",
    "def search(chunks,embeddeds,top_k, txt):\n",
    "    embedding = OpenAIEmbeddings(deployment=\"embedding\",chunk_size=1)\n",
    "    embedded_txt = embedding.embed_query(txt)\n",
    "    distances = [np.dot(embedded_txt,embedded) for embedded in embeddeds]\n",
    "    ret_idx = find_k_largest_indices(distances, top_k)\n",
    "    return [chunks[i] for i in ret_idx]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "187d08ff",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.8023933391562698, 0.8125168324498859, 0.7812473775874645, 0.817075818463568, 0.7898890421834843, 0.8397854867756218, 0.7722375027327882, 0.7687244419032947, 0.8104800333965131, 0.8089354290813963, 0.8057141418303226]\n",
      "[5, 3, 1, 8, 9, 10, 0, 4, 2, 6, 7]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['工作模式和运行特征，优化主机群集。智能弹性集群管理可以有效减少弹性计算资源波动（如：竞价实例的中断）带来的集群服务能力变化。并在面向成本优化构建的混合机型集群最大发挥集群不同机型的服务能力\\n以上各部分',\n",
       " '功实践。\\nSpotMax由以下三个部分组成：\\nMaxArch：构建高可用性，高弹性（Resilient）架构，可以允许系统在不影响业务的前提下更充分的使用具有成本优势的高弹性计算资源（Spot Ins']"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search(chunks,embeddeds,2, \"提高系统可用性\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "6c6a46ac",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.7626223356791848, 0.7574077611525133, 0.7236185893772494, 0.7525226844839109, 0.6965344275382886, 0.748106643696892, 0.7173777528336387, 0.7403726949080747, 0.7204675497717147, 0.7216070546204, 0.7264813507926091]\n",
      "[0, 1, 3, 5, 7, 10, 2, 9, 8, 6, 4]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['SpotMax\\n打破成本约束，释放云端创造力\\n\\n构建大规模云端系统的挑战\\n云计算平台“按需获取”的特性使得用户可以随时获取所需计算资源，实现计算资源与业务规模的动态匹配。云计算时代计算资源已不再是构建',\n",
       " '大规模系统的瓶颈，多数情况下成本成为了企业在构建大规模应用时的最大挑战。\\n\\n什么是 SpotMax?\\nSpotMax是一个利用弹性计算资源进行云端系统成本优化的体系（包括相关产品及解决方案）。Spot']"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search(chunks,embeddeds,2, \"省钱\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "16649e33",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.7592167264242349, 0.7836962526531055, 0.7419871546836885, 0.7720463741599968, 0.8115075334429604, 0.7319843405717201, 0.8144565372167445, 0.6795742540190728, 0.8217077990486893, 0.8230605403147829, 0.7877589772115714]\n",
      "[9, 8, 6, 4, 10, 1, 3, 0, 2, 5, 7]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['能力有差异，智能的动态负载均衡将实现按处理能力的负载分配，实现集群资源利用的最大化。\\nMaxGroup可以根据工作负载变化模式（缓慢变化或急剧变化）和业务需求为系统自动伸缩选择合适的实例类型。 \\nMa',\n",
       " '低中断带来的服务器容量变化带来的服务能力影响，实现服务及数据的提前迁移。\\nMaxGroup将根据现货价格和终止率的变化持续优化集群结构。 \\n支持智能的动态负载均衡，由于集群为多机型混合，不同机型的处理']"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search(chunks,embeddeds,2, \"什么是MaxGroup\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "32755e0c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import openai\n",
    "import numpy as np\n",
    "def anwser_question_with_doc(question, chunks, embeddeds):\n",
    "    relevent_chunks = search(chunks,embeddeds,2, question)\n",
    "    prompt = \"\"\"\n",
    "    仅通过总结以下的文字片段回答用户问题, 注意保持回答的语言通顺（字数在30字以内）\n",
    "    --- \n",
    "    \"\"\"\n",
    "    for rchunks in relevent_chunks:\n",
    "        prompt = prompt + \"\\n'\" + rchunks + \"'\"\n",
    "    \n",
    "    response = openai.ChatCompletion.create(\n",
    "    engine=deployment, # engine = \"deployment_name\".\n",
    "    messages=[\n",
    "            {\"role\": \"system\", \"content\": prompt},   \n",
    "            {\"role\": \"user\", \"content\": question}\n",
    "        ],\n",
    "        temperature = 0.9, \n",
    "        max_tokens = 200\n",
    "   )\n",
    "    return response.choices[0].message.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "cebee7a1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.785118591499548, 0.7976991410829841, 0.7675298917649545, 0.8065815459820063, 0.7816592248948016, 0.8356628134875835, 0.7624604542313403, 0.765247532222735, 0.8019779315035528, 0.8071677645976287, 0.7917167790813946]\n",
      "[5, 9, 3, 8, 1, 10, 0, 4, 2, 7, 6]\n",
      "通过使用智能弹性集群管理优化主机群集和实现动态负载均衡，可以最大化集群资源利用，从而提高可用性。\n"
     ]
    }
   ],
   "source": [
    "print(anwser_question_with_doc(\"如何提高可用性\", chunks,embeddeds))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "9fac75f7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.8770056249839784, 0.8362516140610949, 0.7834500483012314, 0.8301407011787743, 0.7506028306598755, 0.7373641089558522, 0.7637066032721377, 0.7063620631436798, 0.752122314461898, 0.7571780038817336, 0.7157490897923696]\n",
      "[0, 1, 3, 2, 6, 9, 8, 4, 5, 10, 7]\n",
      "SpotMax是一个利用弹性计算资源进行云端系统成本优化的体系，包括相关产品及解决方案。\n"
     ]
    }
   ],
   "source": [
    "print(anwser_question_with_doc(\"SpotMax是什么\", chunks,embeddeds))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "f9db950d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.7849296999456946, 0.7818371614098701, 0.7384494647990351, 0.7937139964158784, 0.7176644252472997, 0.7744640165796035, 0.7446397380953562, 0.7997561198477201, 0.7497060465878663, 0.7364696122979241, 0.7572099874977832]\n",
      "[7, 3, 0, 1, 5, 10, 8, 6, 2, 9, 4]\n",
      "通过对集群中Spot instance中断的预先补偿，防止其中断。\n"
     ]
    }
   ],
   "source": [
    "print(anwser_question_with_doc(\"如何防止spot instance中断\", chunks,embeddeds))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3040d24c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
